<?php

namespace App\Http\Controllers;

use App\Util\Tool;
use Illuminate\Http\Request;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Util\Url;
use Intervention\Image\Facades\Image;

class QrCodeController extends BaseController
{
    //测试使用
    public function storeQrcode(Request $request)
    {
        if (empty($request->work_id)) {
            return $this->ResultJson(0, "工作室id不能为空", []);
        }
        $url = Url::getHttpHost()."home/sign?work_id=" . $request->work_id;
        $name = 'image/' . $request->work_id. '_doctor.png';
        if(file_exists(public_path($name))){
            return $this->ResultJson(1, "获取成功", ["url" => Url::getHttpHost() . $name]);
        }
        $canvas = Image::canvas(800, 900);//画布宽高
        $head = Image::make(public_path('image/bg.png'))
            ->resize(800, 900);//背景图片宽高
        $canvas->insert($head, 'center', 0, 0)//背景图片位置
        ->insert($this->createQrCode($url), 'bottom-center', 0, 190);
        $canvas->save(public_path($name));
        return $this->ResultJson(1, "获取成功", ["url" => Url::getHttpHost() . $name]);
    }

    //病历夹带背景图
    public function dossier(Request $request)
    {
        if (empty($request->workId)) {
            return $this->ResultJson(0, "工作室id不能为空", []);
        }
        if (empty($request->doctorId)) {
            return $this->ResultJson(0, "医生id不能为空", []);
        }
        $url = Url::getHttpHost()."case-history/?workId=" . $request->workId.'&doctorId='.$request->doctorId;
        $name = 'image/' . $request->workId.'-'.$request->doctorId. '_doctor.png';
        if(file_exists(public_path($name))){
            return $this->ResultJson(1, "获取成功", ["url" => Url::getHttpHost() . $name]);
        }
        $canvas = Image::canvas(800, 900);//画布宽高
        $head = Image::make(public_path('image/bg.png'))
            ->resize(800, 900);//背景图片宽高
        $canvas->insert($head, 'center', 0, 0)//背景图片位置
        ->insert($this->createQrCode($url), 'bottom-center', 0, 190);
        $canvas->save(public_path($name));
        return $this->ResultJson(1, "获取成功", ["url" => Url::getHttpHost() . $name]);
    }
    //病历夹不带背景图
    public function dossierandroid(Request $request)
    {
        if (empty($request->workId)) {
            return $this->ResultJson(0, "工作室id不能为空", []);
        }
        if (empty($request->doctorId)) {
            return $this->ResultJson(0, "医生id不能为空", []);
        }
        $url = Url::getHttpHost()."case-history/?workId=" . $request->workId.'&doctorId='.$request->doctorId;
        $name = 'image/' . $request->workId.'-android-'.$request->doctorId. '_doctor.png';
        QrCode::format('png')
            ->size(520)->margin(2)->encoding('UTF-8')
            ->generate($url, public_path($name));
        return $this->ResultJson(1, "获取成功", ["url" => Url::getHttpHost() . $name]);
    }

    public function createQrCode($str)
    {
        return QrCode::format('png')->size(280)->margin(0)->encoding('UTF-8')->generate($str);
    }
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * 门店二维码
     */

    public function storeQrcode_old(Request $request)
    {
        $param = $request->input();
//        if (empty($param['user_id'])) {
//            return $this->ResultJson(0, "用户id不能为空", []);
//        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室id不能为空", []);
        }
        $name = 'image/' . $param['work_id'] . '_doctor.png';
//        QrCode::format('png')->size(520)
//            ->mergeString(file_get_contents(public_path('image/qr_code.png')), 0.1)
//            ->generate('https://chuyuan.baikailun.com/home/sign?id=' . $param['user_id']."&work_id=".$param['work_id']);
        QrCode::format('png')
            ->size(520)->margin(2)->encoding('UTF-8')
            ->generate("https://mains.yiluchuyuan.com/home/sign?work_id=" . $param['work_id'], public_path($name));
        return $this->ResultJson(1, "获取成功", ["url" => Url::getHttpHost() . $name]);
//        return response()->make($byte)->header('content-type', 'image/png');
    }
}
